package rps.client.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import rps.game.data.FigureKind;

public class WeaponChoicePanel extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = -7834047356894050900L;

	private GamePane gamePane;
	
	private JButton rockButton = new JButton("ROCK"),
			paperButton = new JButton("PAPER"),
			scissorsButton = new JButton("SCISSORS");

	public WeaponChoicePanel(GamePane gamePane){
		super();
		this.gamePane = gamePane;
		
		initComponents();
	}
	
	/**
	 * Sets the Layout of the WeaponChoicePanel and adds the buttons.
	 */
	private void initComponents(){
		setLayout(new GridLayout(1,3));
		add(rockButton);
		rockButton.addActionListener(this);
		rockButton.setIcon(new ImageIcon(MediaProvider.getMediaProvider().getImage("rock_hand_own")));
		add(paperButton);
		paperButton.addActionListener(this);
		paperButton.setIcon(new ImageIcon(MediaProvider.getMediaProvider().getImage("paper_hand_own")));
		add(scissorsButton);
		scissorsButton.addActionListener(this);
		scissorsButton.setIcon(new ImageIcon(MediaProvider.getMediaProvider().getImage("scissors_hand_own")));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		enable(false);
		FigureKind fk = null;
		if(e.getSource().equals(rockButton)){
			fk = FigureKind.ROCK;
		}
		else if(e.getSource().equals(paperButton)){
			fk = FigureKind.PAPER;
		}
		else if(e.getSource().equals(scissorsButton)){
			fk = FigureKind.SCISSORS;
		}
		if(gamePane.uiController.getPlacementPhase()!=-1){
			gamePane.setStatusText("Warte auf Gegner...");			
			gamePane.gameController.setAssignment(gamePane.getInitialFigures());
			gamePane.gameController.initialChoice(fk);
			gamePane.enableButtons(false);
			gamePane.enableInitialAssignmentPanel(false);
		}
		else{
			gamePane.setStatusText("Warte auf Gegner...");
			gamePane.gameController.drawChoice(fk);
		}
	}
	
	/**
	 * Enables the RPS Buttons
	 */
	@Override
	public synchronized void enable(boolean enable){
		rockButton.setEnabled(enable);
		paperButton.setEnabled(enable);
		scissorsButton.setEnabled(enable);
	}

	public void reset() {
		// nothing to reset
	}
}
